## author:    A. D?r, Robert A. Huber, Gemma Mateo, Gabriele Spilker
## contact:   robert.huber@sbg.ac.at
## file name: nti_experiment_descriptive.R
## Context:   Project on NTI in PTAs
## started:   2020-12-14
## Summary:   Provides core descirptives

df_exp %>%
  filter(!is.na(rating)) %>%
  group_by(rating) %>%
  summarise(n = n()) %>%
  mutate(share = n/sum(n)) %>%
  mutate(dim = "a) Overall rating") %>%
  ggplot(., aes(x = rating, y = share)) + 
  geom_bar(stat = "identity") + 
  theme_minimal() + 
  facet_wrap(.~dim, scales = "free_x", nrow = 2) +
  labs(x = "\nRating", y = "Share\n") +
  scale_x_continuous(breaks = c(1:10)) + 
  scale_y_continuous(breaks = c(0, .1, .2), limits = c(0,.2))

ggsave("./output/Figure1a.eps",
       device = "eps",
       width = 20,
       height = 7,
       units = "cm")


data.frame(dim = "b) Groups", 
           cat = as.character(df_exp$ig),
           rat = df_exp$rating) %>%
  filter(!is.na(cat)) %>%
  group_by(dim, cat, rat) %>%
  summarise(n = n()) %>%
  mutate(share = n/sum(n)) %>%
  ungroup() %>%
  mutate(cat = factor(cat,
                      levels = c("Other business",
                                 "Export oriented business",
                                 "Import competing business",
                                 "Citizen groups",
                                 "Labour unions"),
                      labels = c("Other business",
                                 "Export-oriented business",
                                 "Import-competing business",
                                 "Citizen groups",
                                 "Labour unions"))) %>%
  ggplot(., aes(x = rat, y = share, fill = cat)) + 
  geom_bar(stat = "identity", position = position_dodge()) + 
  theme_minimal() + 
  facet_wrap(.~dim, scales = "free_x", nrow = 2) +
  theme(legend.position = c(0.8, 0.8),
        legend.background = element_rect("white"),
        legend.title = element_blank()) + 
  scale_fill_discrete("Groups:") + 
  labs(x = "\nRating", y = "Share\n") +
  scale_x_continuous(breaks = c(1:10)) + 
  scale_y_continuous(breaks = c(0, .1, .2))

ggsave("./output/Figure1b.eps",
       device = "eps",
       width = 20,
       height = 10,
       units = "cm")

psych::describeBy(df_exp$rating, df_exp$ig)


df_exp %>%
  select(rating, choice,
         ser, ipr, env, lab) %>%
  mutate(ser = as.numeric(ser)-1,
         ipr = as.numeric(ipr)-1,
         lab = as.numeric(lab)-1,
         env = as.numeric(env)-1) %>%
  stargazer::stargazer(as.data.frame(.), out = "./output/TableA1.tex",type = "latex", digits = 2, header = F, out.header = F)

df %>%
  select(gen_type, non_EU_OECD_focus, shr, focus_region1, general_business, services_tradeable, bus_sector, ngo_type, knowledge_intensive) %>%
  mutate(imp_bus = ifelse(gen_type == "Business groups" & shr <= .45 & focus_region1 != "world" & general_business == 0, 1, 0),
         exp_bus = ifelse(gen_type == "Business groups" & shr >= .55 & focus_region1 != "world" & general_business == 0, 1, 0),
         oth_bus = ifelse(imp_bus == 1 | exp_bus == 1, 0, 1),
         cit = ifelse(gen_type == "Citizen groups", 1, 0),
         lab = ifelse(gen_type == "Labour union", 1, 0),
         non_EU_OECD_focus = as.numeric(non_EU_OECD_focus)-1,
         northern_bus = ifelse(exp_bus == 1 & non_EU_OECD_focus == 0, 1, 0),
         southern_bus = ifelse(exp_bus == 1 & non_EU_OECD_focus == 1, 1, 0),
         service_trad = ifelse(services_tradeable == 1, 1, 0),
         health_ngo = ifelse(ngo_type == "Health NGO", 1, 0),
         knowledge = ifelse(knowledge_intensive == 1, 1, 0),
         env_ngo = ifelse(ngo_type == "Environmental NGO", 1, 
                          ifelse(is.na(ngo_type) | ngo_type != "Environmental NGO", 0, NA)),
         lab_cit = ifelse(ngo_type %in% c("Development NGO", "Human rights NGO", "Social welfare NGO"), 1, 0))%>%
  select(oth_bus, imp_bus, exp_bus, cit, lab, non_EU_OECD_focus, northern_bus, southern_bus, services_tradeable, health_ngo,
         knowledge, env_ngo, lab_cit) %>%
  stargazer::stargazer(as.data.frame(.), out = "./output/TableA2.tex", type = "latex", digits = 2, header = F, out.header = F)

df_exp %>%
  select(gen_type, non_EU_OECD_focus) %>%
  group_by(gen_type, non_EU_OECD_focus) %>%
  summarise(n = n()/4,
            s = n()/nrow(df_exp))

df_exp %>%
  select(gen_type) %>%
  group_by(gen_type) %>%
  summarise(n = n()/4,
            s = n()/nrow(df_exp))

df_exp %>%
  select(non_EU_OECD_focus) %>%
  group_by(non_EU_OECD_focus) %>%
  summarise(n = n()/4,
            s = n()/nrow(df_exp))

